import functools
class Solution(object):
    def scoreOfStudents(self, s, answers):
        nums = []
        op = []
        for c in s:
            if c in {'+', '*'}:
                op.append(c)
            else:
                nums.append(int(c))
        
        @functools.cache
        def dfs(i, j):
            if i == j:
                return {nums[i]}
            ans = set()
            for k in range(i, j):
                left = dfs(i, k)
                right = dfs(k + 1, j)
                for num1 in left:
                    for num2 in right:
                        if op[k] == '+':
                            if num1 + num2 <= 1000:
                                ans.add(num1 + num2)
                        else:
                            if num1 * num2 <= 1000:
                                ans.add(num1 * num2)
            return ans
        m = len(nums)
        ans_set = dfs(0, m - 1)
        st = [nums[0]]
        for i in range(1, m):
            if op[i - 1] == '+':
                st.append(nums[i])
            else:
                st[-1] *= nums[i]
        true_value = sum(st)
        
        ans = 0
        for t in answers:
            if t == true_value:
                ans += 5
            else:
                if t in ans_set:
                    ans += 2
        return ans



data = Solution()
s = "6+8*6+8+2*6+6*2+8*6+8*2+8+8*8+6"
answers = [20,13,42]
print(data.scoreOfStudents(s, answers))